package com.zhiyou100.taxi.acction;

import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.dao.CarOutMapper;
import com.zhiyou100.taxi.entity.CarOut;
import com.zhiyou100.taxi.service.CarOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashSet;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-25 21:38
 * @Description
 */
@RequestMapping("/carout")
@Controller
public class CarOutController {
    @Autowired
    CarOutService carOutService;

    @RequestMapping("/show.action")
    public String show() {
//        return "forward:/template/busines/carInList.jsp";
//        return "busines/carInList";
        return "forward:/carout/page.action";
    }

    @RequestMapping("/page.action")
    public String page(Integer pageNum, HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<CarOut> pageInfo = carOutService.queryPage(pageNum);
        request.getSession().setAttribute("carOutPageInfo", pageInfo);
        return "busines/caOutList";
    }

    @RequestMapping("/getOne.action")
    public String getOne(String carOutId, HttpServletRequest request) {
        CarOut carOut = carOutService.getByPrimaryKey(carOutId);
        request.setAttribute("carOut", carOut);
        return "busines/car-out-edit";
    }

    @RequestMapping("/save.action")
    public String save(CarOut carOut, HttpServletRequest request) {
        System.out.println("保存的数据===》carOut = " + carOut);
        carOut.setCarOutId(carOutService.getPrimaryKey());
        int num = carOutService.insert(carOut);
        if (num > 0) {
            request.setAttribute("msg", "保存成功");
        } else {
            request.setAttribute("smg", "保存失败");
        }
        return "forward:/carout/page.action";
    }

    @RequestMapping("/update.action")
    public String update(CarOut carOut, HttpServletRequest request) {
        int num = carOutService.updateByPrimaryKey(carOut);
        if (num > 0) {
            request.setAttribute("msg", "修改成功");
        } else {
            request.setAttribute("smg", "修改失败");
        }
        return "forward:/carout/page.action";
    }

    @RequestMapping("/deleteOne.action")
    public String deleteOne(String carOutId, HttpServletRequest request) {
        int num = carOutService.deleteByPrimaryKey(carOutId);
        if (num > 0) {
            request.setAttribute("msg", "删除成功");
        } else {
            request.setAttribute("smg", "删除失败");
        }
        return "forward:/carout/page.action";
    }

    @RequestMapping("/deleteBatch.action")
    public String deleteBatch(String carOutIds, HttpServletRequest request) {
        String[] split = carOutIds.split(",");
        HashSet<String> set = new HashSet<>();
        set.addAll(Arrays.asList(split));
        System.out.println("要删除的数据=====》" + set);
        int num = carOutService.deleteByPrimaryKeyS(set);
        if (num > 0) {
            request.setAttribute("msg", "批量删除成功");
        } else {
            request.setAttribute("smg", "批量删除失败");
        }
        return "forward:/carout/page.action";
    }
}
